package se.peterbjorkman.android.trafikkamera;

import android.content.Context;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class InfoBlock extends LinearLayout {
    private static final String LOGTAG = InfoBlock.class.getName();
    
    TextView mTitleTextView;
    TextView mInfoTextView;
    LinearLayout mLayout;
    int mTextSize = 12;

    public InfoBlock(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        Log.d(LOGTAG, "In Constrctor Context context, AttributeSet attributeSet");
        init(context);
    }

    public InfoBlock(Context context) {
        super(context);
        Log.d(LOGTAG, "In Constrctor Context context");
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.info_block_layout, this);
        mTitleTextView = (TextView) findViewById(R.id.TitleTextView);
        mInfoTextView = (TextView) findViewById(R.id.InfoTextView);
        mLayout = (LinearLayout) findViewById(R.id.LinearLayout01);
    }
    
    public CharSequence getTitleText() {
        Log.d(LOGTAG, "In getTitleText");
        return mTitleTextView.getText();
    }

    public void setTitleText(CharSequence text) {
        Log.d(LOGTAG, "In setTitleText");
        mTitleTextView.setText(text);
    }

    public CharSequence getInfoText() {
        Log.d(LOGTAG, "In getInfoText");
        return mInfoTextView.getText();
    }

    public void setInfoText(CharSequence text) {
        Log.d(LOGTAG, "In setInfoText");
        mInfoTextView.setText(text);
    }
    
    public void SetInfoTextSize(int size){
        Log.d(LOGTAG, "In SetInfoTextSize");
        mInfoTextView.setTextSize(size);
        mTextSize = size;
    }

    public void addExtraText(CharSequence text, Context context){
        addExtraText(text, context, true);
    }
    
    public void addExtraText(CharSequence text, Context context, Boolean autoLinks){
        TextView newTextView = new TextView(context);
        newTextView.setText(text);
        newTextView.setTextSize(mTextSize);
        setLinksClickable(autoLinks, newTextView);
        mLayout.addView(newTextView);
    }
    
    public void addText(CharSequence text, Context context){
        CharSequence currentMainText = mInfoTextView.getText();
        if (currentMainText == null || currentMainText.length() == 0)
            setInfoText(text);
        else
            addExtraText(text, context);
    }
    
    public void setUseLinks(Boolean useLinks){
        setLinksClickable(useLinks, mInfoTextView);
    }

    private void setLinksClickable(Boolean useLinks, TextView view) {
        view.setLinksClickable(useLinks);
        int mask = 0;
        if (useLinks)
            mask = Linkify.ALL; 
        view.setAutoLinkMask(mask);
    }
}
